#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

pthread_t ntid;

void printids(char *s){
    pid_t pid = getpid();
    pthread_t tid = pthread_self();

    printf("%s pid %lu  tid %lu (0x%lx)\n", s, (unsigned long)pid,
        (unsigned long)tid, (unsigned long)tid);
}

void *thr_fn(void *arg){
    printids("new thread: ");
    int j = 1;
    for(int i = 2; i < 100; i++){
        j *= i;
    }
    return NULL;
}

int main(){
    int err;
    for(int i = 0; i < 10000; i++){
        err = pthread_create(&ntid, NULL, thr_fn, NULL);
    }
    while(1){
        sleep(2);
        printf("i am sleep\n");
    }
    if(err != 0){
        printf("pthread_create error\n");
        exit(0);
    }
    printids("main thread: ");
    sleep(1);       //如果不睡眠，主线程可能直接退出而新线程没有机会运行。
    return 0;

}